<?php
class Admin_ArticlesController extends My_Controller_Admin_Choix
{
	public function indexAction()
	{
	    $numVente = $this->_getParam('num_vente');
		$rowPerPage = $this->_getParam('rowperpage', 20);
		$currentPage = 1;
		$page = $this->_getParam('page', 1);
		if(!empty($page)) {
			$currentPage = $page;
		}

		$model =  Mage::getModel('admin/articles');
		$collection = $model->getCollection($this->view->numVente);
		$paginator = Zend_Paginator::factory($collection);
		$paginator->setItemCountPerPage($rowPerPage);
		$paginator->setCurrentPageNumber($currentPage);

		$this->view->page = $page;
		$this->view->paginator = $paginator;
		$this->view->checkRenum = $model->checkRenum($numVente);
	}

	public function editAction()
	{
	    $id = $this->_getParam('id', 0);
	    $object = Mage::getModel('admin/articles');
	    $data = array();
	    if ($id) {
	        $data = $object->getArticle($id);
	    }
	    $this->view->object = new My_Object($data);
	}

	public function newAction()
	{
	    $this->_forward('edit');
	}

	public function saveAction()
	{
	    $id = $this->_getParam('id', null);
	    $model = new Admin_Model_Articles();
	    $data = $this->_getParam('data');
	    if ($data['commission'] == '') {
	        $data['commission'] = 99;
	    }
	    if (!$id) {
	        $data['p_articles'] = $model->getMaxId() + 1;
	        $offrants = $this->_getParam('offrant');
	        foreach ($offrants as $item) {
	            if ($item['f_clients'] != 0) {
	                $item['f_articles'] = $data['p_articles'];
	                $model->insertArticleOffrant($item);
	            }
	        }
	    }
	    $model->save($data, $id);
	    $this->_redirect('admin/articles/?num_vente=' . $this->view->numVente);
	    exit;
	}

	public function deleteAction()
	{
	    $id = $this->_getParam('id');
	    $article = Mage::getModel('admin/articles')->load($id);
	    $article->remove($id)->deleteOffrants($id);
	    $this->_redirect('admin/articles/?num_vente=' . $this->view->numVente);
	    exit;
	}

	public function searchAction()
	{
	    $numArticle = $this->_getParam('num');
	    $numVente = $this->_getParam('num_vente');
        if ($id = Mage::getModel('admin/articles')->searchArticle($numArticle, $numVente)) {
            $params = '/id/' . $id;
            $params .= '/?num_vente=' . $numVente;
            $this->_redirect('admin/articles/edit' . $params);
        } else {
            $this->_goBack();
        }
	}

	public function exportCsvAction()
	{
	    $numVente = $this->_getParam('num_vente');
	    $rows = Mage::getModel('admin/articles')->getCsvCollection($numVente);
        if ($rows) {
            $filePath = PUBLIC_PATH . '/csv/articles.csv';
            //echo PUBLIC_PATH; exit;
            if (is_writeable($filePath)) {
                $fp = fopen($filePath, 'w');
                foreach ($rows as $row) {
                    fputcsv($fp, $row, ';');
                }
                header('Content-type: application/csv');
                header('Content-Disposition: attachment; filename="articles.csv"');
                readfile($filePath);
            } else {
                echo 'File not writeable';
            }
            exit;
        }
	}
}

